package apple.encryptedNode.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class OperationDAO {
	private EncryptedNodeOpenHelper dbHelper;
	private SQLiteDatabase db;

	public OperationDAO(Context context) {
		dbHelper = new EncryptedNodeOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public Cursor query() {
		Cursor cursor = db.query(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				null, null, null, null, null, null);
		return cursor;

	}

	public long save(String fileName, String fileContent) {
		ContentValues cv = new ContentValues();
		cv.put(EncryptedNodeOpenHelper.KEY_NAME, fileName);
		cv.put(EncryptedNodeOpenHelper.KEY_CONTENT, fileContent);
		long result = db.insert(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				null, cv);
		return result;

	}

	public void update(int id, String fileName, String fileContent) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		// String where = EncryptedNodeOpenHelper.KEY_NAME + "= ?";
		String where = "_id = ?";
		String[] wherevalues = { id + "" };

		ContentValues cv = new ContentValues();
		cv.put(EncryptedNodeOpenHelper.KEY_NAME, fileName);
		cv.put(EncryptedNodeOpenHelper.KEY_CONTENT, fileContent);

		db.update(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME, cv, where,
				wherevalues);

	}

	public void delete(int id) {
		String where = " _id = ?";
		String[] wherevalues = { id + "" };
		db.delete(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME, where,
				wherevalues);

	}
	public void close(){
		db.close();
	}
}
